package com.lagou.transfer.service.impl;

import com.lagou.transfer.dao.BankDao;
import com.lagou.transfer.pojo.Bank;
import com.lagou.transfer.pojo.dto.TransferDto;
import com.lagou.transfer.service.BankService;
import com.spring.annotation.Autowired;
import com.spring.annotation.Service;
import com.spring.annotation.Transactional;

import java.util.List;

@Service
public class BankServiceImpl implements BankService {
    @Autowired
    private BankDao bankDao;

    @Override
    public List<Bank> select() {
        return bankDao.select();
    }

    @Override
    @Transactional
    public void transfer(TransferDto transferDto) {
        Bank remitter = bankDao.selectOne(transferDto.getRemitterCardNo());
        remitter.setMoney(remitter.getMoney() - transferDto.getMoney());
        bankDao.update(remitter);
//        int a = 1/0;
        Bank payee = bankDao.selectOne(transferDto.getPayeeCardNo());
        payee.setMoney(payee.getMoney() + transferDto.getMoney());
        bankDao.update(payee);

    }

    @Override
    public String toString() {
        return "BankServiceImpl{" +
                "bankDao=" + bankDao +
                '}';
    }
}
